package com.example.community.rest;

import com.example.community.dto.RegisterUserDto;
import com.example.community.model.User;
import com.example.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RequestMapping("/authorize")
@RestController
public class RegisterResource {
    @Autowired
    private UserService userService;


    @PostMapping("/register")
    public void register(@Valid @RequestBody RegisterUserDto registerUserDto) throws HttpRequestMethodNotSupportedException {
        User user = new User();
        user.setPhone(registerUserDto.getPhone());
        user.setPassword(registerUserDto.getPassword());
        user.setPicUrl("http://pic.haidacommunity.cn/default-avatar.png");
        user.setName("user" + (System.currentTimeMillis() % 1000000000));
        userService.createUser(user);
    }
}
